import java.util.Scanner;
class Test{
    //定义两个静态数据成员
    public static int[] countT = {0};
    public static int[] countF = {0};
    //定义一个二维数组，存储变元可能的取值

    //统计T和F的数量
    public static void count(char c){
        if(c == 'T')countT[0]++;
        else if(c == 'F')countF[0]++;
    }

    //输出真值表
    public static void outPutTruthTable(int[][] arr, char[] valRet, int col, int row){

        //真值表第一行，输出变元
        char ch = 'P';
        for(int i = 1; i <= col; i++){
            System.out.printf("%-5c",ch++);
            if(i == col){
                System.out.printf("%-5c\n",'A');
            }
        }

        //真值表第二行，分割线
        for(int i = 1; i <= col; i++){
            System.out.print("------");
        }
        System.out.println();

        //其余各行，不同变元可能的取值
        ch = 'P';
        for(int i = 1; i <= row; i++){
            for(int j = col; j >= 1;j--){
                System.out.printf("%-5c", arr[i-1][j-1] == 1 ? 'T' : 'F');
            }
            System.out.printf("%-5c\n", valRet[i-1]);
        }
    }

    //主析取范式
    public static void masterDisjunction(int[][] arr, char[] valRet, int col , int row, int countT, int[] count){
        for (int i = 1; i <= row; i++) {
            if(valRet[i-1] == 'T'){
                count[0]++;
                System.out.print("(");
                char ch = 'P';
                for (int j = col; j >= 1; j--){
                    System.out.print(arr[i-1][j-1] == 1 ? ch : "┓" + ch);
                    if(j != 1){
                        System.out.print("∧");
                    }
                    ch++;
                }
                System.out.print(")");
                if(count[0] < countT){
                    System.out.print("∨");
                }
            }
        }
    }

    //主合取范式
    public static void masterConjunction(int[][] arr, char[] valRet, int col , int row, int countT, int[] count){
        for (int i = 1; i <= row; i++) {
            if(valRet[i-1] == 'F'){
                count[0]++;
                System.out.print("(");
                char ch = 'P';
                for (int j = col; j >= 1; j--){
                    System.out.print(arr[i-1][j-1] == 0 ? ch : "┓" + ch);
                    if(j != 1){
                        System.out.print("∨");
                    }
                    ch++;
                }
                System.out.print(")");
                if(count[0] < countT){
                    System.out.print("∧");
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        //输入变元个数
        System.out.println("请输入变元的个数:");
        int n = in.nextInt();

        //输入真值结果
        int line = (int) Math.pow(2, n);
        char[] valRet = new char[line];
        System.out.println("请输入" + line + "个真值结果（T和F表示）:");

        for (int i = 0; i < line; i++) {
            valRet[i] = in.next().charAt(0);
            count(valRet[i]);
        }
        System.out.println();

        //真值表赋值
        int [][] arr= new int[line][n];
        int col = n;
        int row = line;

        //真值表命题变元赋值
        for (int i = 0; i < line; i++) {
            int value = i;
            for (int j = 0; j < n; j++) {
                arr[i][j] = value % 2;
                value /= 2;
            }
        }

        //真值表输出
        System.out.println("公式对应的真值表:");
        outPutTruthTable(arr, valRet, col ,row);
        System.out.println();

        //主析取范式输出
        int[] count = {0};
        System.out.println("主析取范式");
        masterDisjunction(arr, valRet,col,row,countT[0],count);
        System.out.println();

        //主合取范式输出
        System.out.println("主合取范式");
        masterConjunction(arr, valRet,col,row,countF[0],count);
        System.out.println();
    }
}

/*import java.util.Scanner;
public class Test {
    // 计数真值结果
    static void countTF(char c, int[] countT, int[] countF) {
        if (c == 'T') {
            countT[0]++;
        } else if (c == 'F') {
            countF[0]++;
        }
    }
    // 输出真值表
    static void outputTruthTable(int[][] truthTable, char[] valueRet, int col, int row) {
        char ch = 'P';
        for (int i = 1; i <= col; i++) {
            System.out.printf("%-10c", ch++);
            if (i == col) {
                System.out.printf("%-10c\n", 'A');
            }
        }
        for (int i = 1; i <= col; i++) {
            System.out.print("-----------");
        }
        System.out.println();
        ch = 'P';
        for (int i = 1; i <= row; i++) {
            for (int j = col; j >= 1; j--) {
                System.out.printf("%-10c", truthTable[i - 1][j - 1] == 1 ? 'T' : 'F');
            }
            System.out.printf("%-10c\n", valueRet[i - 1]);
        }
    }
    // 主析取范式
    static void masterDisjunction(int[][] truthTable, char[] valueRet, int col, int row, int countT, int[] count) {
        for (int i = 1; i <= row; i++) {
            if (valueRet[i - 1] == 'T') {
                count[0]++;
                System.out.print("(");
                char ch = 'P';
                for (int j = col; j >= 1; j--) {
                    System.out.print(truthTable[i - 1][j - 1] == 1 ? ch : "┓" + ch);
                    if (j != 1) {
                        System.out.print("∧");
                    }
                    ch++;
                }
                System.out.print(")");
                if (count[0] < countT) {
                    System.out.print("∨");
                }
            }
        }
    }
    // 主合取范式
    static void masterConjunction(int[][] truthTable, char[] valueRet, int col, int row, int countF, int[] count) {
        for (int i = 1; i <= row; i++) {
            if (valueRet[i - 1] == 'F') {
                count[0]++;
                System.out.print("(");
                char ch = 'P';
                for (int j = col; j >= 1; j--) {
                    System.out.print(truthTable[i - 1][j - 1] == 0 ? ch : "┓" + ch);
                    if (j != 1) {
                        System.out.print("∨");
                    }
                    ch++;
                }
                System.out.print(")");
                if (count[0] < row) {
                    System.out.print("∧");
                }
            }
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 1 输入变量个数
        System.out.print("请输入变量个数:> ");
        int n = scanner.nextInt();
        System.out.println();
        // 2 输入真值结果
        int totalNumber = (int) Math.pow(2, n); // 共2^n个真值结果
        char[] valueRet = new char[totalNumber];
        int[] countT = {0};
        int[] countF = {0};
        System.out.print("请输入" + totalNumber + "个真值结果(有效字符为 T 和 F，以回车结束) : ");
        for (int i = 0; i < totalNumber; i++) {
            valueRet[i] = scanner.next().charAt(0);
            countTF(valueRet[i], countT, countF);
        }
        System.out.println();
        // 3 真值表赋值
        int[][] truthTable = new int[totalNumber][n];
        int col = n;
        int row = totalNumber;
        // 真值表命题变元赋值
        for (int i = 0; i < totalNumber; i++) {
            int truthValue = i;
            for (int j = 0; j < n; j++) {
                truthTable[i][j] = truthValue % 2; // 存入1或0
                truthValue /= 2;
            }
        }
        // 4 真值表输出
        System.out.println("公式对应的真值表:> ");
        outputTruthTable(truthTable, valueRet, n, totalNumber);
        System.out.println();
        // 5 主析取范式输出
        int[] count = {0};
        System.out.println("主析取范式:");
        masterDisjunction(truthTable, valueRet, n, totalNumber, countT[0], count);
        System.out.println();
        // 6 主合取范式输出
        System.out.println("主合取范式:");
        masterConjunction(truthTable, valueRet, n, totalNumber, countF[0], count);
        System.out.println();
        scanner.close();
    }
}*/